var sskc = {
    setBtnStatus: function () {
        var row = $('#grid_sskc').datagrid('getSelected');
        if (row && row.qk == 0) {
            $("#sskc_kccbtz").show();
        } else {
            $("#sskc_kccbtz").hide();
        }
        if (row && row.qk) {
            $("#sskc_qk").hide();
            $("#sskc_qx").show()
        } else if (row && !row.qk) {
            $("#sskc_qk").hide();
            $("#sskc_qx").hide()
        } else {
            $("#sskc_qk").hide();
            $("#sskc_qx").hide()
        }
    },
    alwayShowDetail: function (rowData) {
        $('#layout_sskc').layout('remove', 'center');
        $('#layout_sskc').layout('add', {
            region: 'center',
            split: true,
            href: $.webPath() + '/kcgl/sskc/sskc_detail?khId=' + rowData.id
        });
    },
    showDetail: function (rowData) {
        if (rowData && $("#sskc_detail_id").val() != rowData.id) {
            sskc.alwayShowDetail(rowData);
        }
    }
}
$(function () {
    //表格初始加载
    $('#grid_sskc').datagrid({
        url: $.webPath() + "/kcgl/sskc/sskcList",
        toolbar: "#toolbar_sskc",
        fitColumns: false,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        checked: true,
        checkOnSelect: true,
        selectOnCheck: false,
        striped: true,
        showFooter: true,
        frozenColumns: [
            [
                {field: 'checked', title: '选择', align: 'center', checkbox: true},
                {field: 'chtbm', align: 'center', title: '采购合同号', width: 120, align: 'center',
                    formatter: function (value, row) {
                        if (value) {
                            return '<a style="color: red" href="javascript:void(0)" onclick="sskcDetail(' + row.id + ')">' + value + '</a>'
                        }
                    }
                },
                {field: 'xhtbm', align: 'center', width: 120, title: '销售合同号'},
                {field: "qdrq", align: 'center', width: 80, title: "签订日期"},
                {field: "xsdh", align: 'center', width: 100, title: "销售合同子项号"},
                {field: "carno", align: 'center', width: 80, title: "车皮号"},
                {field: "bindno", align: 'center', width: 100, title: "捆包号"},
                {field: "fhrq", align: 'center', width: 80, title: "发货日期"}

            ]
        ],
        columns: [
            [
                {field: 'name', align: 'center', width: 80, title: '品名'},
                {field: 'cz', align: 'center', width: 80, title: '材质'},
                {field: 'ggxh', align: 'center', width: 80, title: '规格型号'},
                {field: 'cd', align: 'center', width: 70, title: '产地'},
                {field: 'dw', align: 'center', width: 40, title: '单位'},
                {field: 'rkjs', align: 'center', width: 80, title: '入库件数'},
                {
                    field: 'rksl', align: 'center', width: 80, title: '入库重量',
                    formatter: function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }
                },
                {field: 'js', align: 'center', width: 80, title: '剩余件数'},
                {
                    field: 'kkdsl', title: '剩余重量', width: 80, align: 'center',
                    formatter: function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }
                },
                {
                    field: 'wsdj', align: 'center', width: 80, title: '合同单价',
                    formatter: function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }
                },
                {
                    field: 'sjdj', align: 'right', width: 80, title: '实际单价',
                    formatter: function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }
                },
                {
                    field: 'wsje', align: 'right', width: 80, title: '实际金额',
                    formatter: function (value) {
                        var val = formatJeForHtml2(value);
                        return val;
                    }
                },
                {field: 'ck', align: 'center', width: 80, title: '仓库'},
                {field: "rkrq", align: 'center', width: 80, title: "入库日期"},
                {field: "rkts", align: 'center', width: 80, title: "入库天数"},
                {field: "ckrq", align: 'center', width: 80, title: "出库日期"}
            ]
        ],
        onClickRow: function () {
            sskc.setBtnStatus();
        }
    });

    //**********按钮触发事件**********
    //查询按钮
    $("#btn_sskc_search").click(function () {
        sskc_search();
    });

    $("#sskc_export").click(function () {
        var chtbm = $('#sskc_chtbm').val();
        var xhtbm = $('#sskc_xhtbm').val();
        var qdrqstart = $('#sskc_qdrq_start').datebox('getValue');
        var qdrqend = $('#sskc_qdrq_end').datebox('getValue');
        var xsxt_xsdh = $('#sskc_xsxt_xsdh').val();
        var carno = $('#sskc_carno').val();
        var bindno = $('#sskc_bindno').val();
        var fhrq = $('#sskc_fhrq').datebox('getValue');
        var ckrqstart = $('#sskc_ckrq_start').datebox('getValue');
        var ckrqend = $('#sskc_ckrq_end').datebox('getValue');
        var sskczt = $("#toolbar_sskc input[name='sskc_zt']").val();
        window.open($.webPath() + "/kcgl/sskc/sskc_export?chtbm=" + chtbm + "&xhtbm=" + xhtbm + "&qdrqstart=" + qdrqstart
            + "&qdrqend=" + qdrqend + "&xsxt_xsdh=" + xsxt_xsdh + "&carno=" + carno
            + "&bindno=" + bindno + "&fhrq=" + fhrq + "&ckrqstart=" + ckrqstart + "&ckrqend=" + ckrqend + "&sskczt=" + sskczt);
    });
});

// 详细
function sskcDetail(khId) {
    $.window({
        title: "详细",
        iconCls: 'icon-tip',
        url: $.webPath() + '/kcgl/sskc/sskc_detail?khId=' + khId,
        width: 1200,
        height: 500
    });
}

//实时库存查询
function sskc_search() {
    var chtbm = $('#sskc_chtbm').val();
    var xhtbm = $('#sskc_xhtbm').val();
    var qdrqstart = $('#sskc_qdrq_start').datebox('getValue');
    var qdrqend = $('#sskc_qdrq_end').datebox('getValue');
    var xsxt_xsdh = $('#sskc_xsxt_xsdh').val();
    var carno = $('#sskc_carno').val();
    var bindno = $('#sskc_bindno').val();
    var fhrq = $('#sskc_fhrq').datebox('getValue');
    var ckrqstart = $('#sskc_ckrq_start').datebox('getValue');
    var ckrqend = $('#sskc_ckrq_end').datebox('getValue');
    var zt = $("#toolbar_sskc input[name='sskc_zt']").val();
    $('#grid_sskc').datagrid('load',
        {
            chtbm: chtbm,
            xhtbm: xhtbm,
            qdrqstart: qdrqstart,
            qdrqend: qdrqend,
            xsxt_xsdh: xsxt_xsdh,
            carno: carno,
            bindno: bindno,
            fhrq: fhrq,
            ckrqstart: ckrqstart,
            ckrqend: ckrqend,
            sskczt: zt
        }
    );
}


